Skip to content

fix: improve badge text measurement accuracy#1486

Merged
ghostdevv merged 1 commit intonpmx-dev:mainfrom
wojtekmaj:text-measurement
Feb 14, 2026
Merged

fix: improve badge text measurement accuracy#1486
ghostdevv merged 1 commit intonpmx-dev:mainfrom
wojtekmaj:text-measurement

Conversation

@wojtekmaj
Copy link
Contributor

This PR improves badge appearance by using a more accurate method to measure text width. Instead of relying on a predefined character width, it utilizes the Canvas API to measure the actual width of the text, resulting in better alignment and spacing for badges with varying character widths.

Speaking in code, this is how the old and new methods compare:

{ label: 'iiiiiiiiii', leftWidthOld: 86, leftWidth: 41 }
{ label: 'wwwwwwwwww', leftWidthOld: 86, leftWidth: 97 }

Actual images before:
iiiold
wwwold

After:
iiinew
wwwnew

Copilot AI review requested due to automatic review settings February 13, 2026 23:54
@vercel
Copy link

vercel bot commented Feb 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs.npmx.dev Ready Ready Preview, Comment Feb 14, 2026 0:44am
npmx.dev Ready Ready Preview, Comment Feb 14, 2026 0:44am
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
npmx-lunaria Ignored Ignored Feb 14, 2026 0:44am

Request Review

@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the accuracy of SVG badge sizing by measuring text width using a Canvas 2D context (instead of a fixed per-character heuristic), which should improve alignment/spacing for variable-width text.

Changes:

  • Add Canvas-based text measurement with caching + heuristic fallback for badge width calculations.
  • Simplify badge width calculation by removing the per-strategy character-width override.
  • Add @napi-rs/canvas dependency (and lockfile updates) to support server-side Canvas measurement.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
server/api/registry/badge/[type]/[...pkg].get.ts Switch badge text width calculation to Canvas measureText, add cached context + fallback.
package.json Add @napi-rs/canvas dependency.
pnpm-lock.yaml Lockfile updates for @napi-rs/canvas and its optional platform packages.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 13, 2026

Warning

Rate limit exceeded

@wojtekmaj has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 23 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Adds dependency @napi-rs/canvas and replaces fixed per-character badge width calculations with canvas-based text measurement in the badge endpoint. Introduces a cached SKRS 2D canvas context, a unified measureTextWidth that uses that context, a fallback measurement path when canvas is unavailable, and constants for badge padding, minimum text width and font shorthand. Updates right-side width calculation to use the new measurement flow while preserving existing left-side logic.

Possibly related PRs

Suggested reviewers

  • danielroe
🚥 Pre-merge checks | ✅ 1 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Merge Conflict Detection ⚠️ Warning ❌ Merge conflicts detected (16 files):

⚔️ app/components/Org/OperationsQueue.vue (content)
⚔️ app/components/Package/ClaimPackageModal.vue (content)
⚔️ app/components/Package/TrendsChart.vue (content)
⚔️ app/components/Package/Versions.vue (content)
⚔️ app/components/Package/WeeklyDownloadStats.vue (content)
⚔️ app/components/Readme.vue (content)
⚔️ app/composables/useCharts.ts (content)
⚔️ app/pages/compare.vue (content)
⚔️ app/pages/package/[[org]]/[name].vue (content)
⚔️ i18n/locales/en.json (content)
⚔️ i18n/schema.json (content)
⚔️ lunaria/files/en-GB.json (content)
⚔️ lunaria/files/en-US.json (content)
⚔️ modules/runtime/server/cache.ts (content)
⚔️ package.json (content)
⚔️ pnpm-lock.yaml (content)

These conflicts must be resolved before merging into main.
Resolve conflicts locally and push changes to this branch.
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The PR description clearly explains the improvement: replacing fixed character width with Canvas API-based text measurement for better badge alignment and spacing.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

This PR improves badge appearance by using a more accurate method to measure text width. Instead of relying on a predefined character width, it utilizes the Canvas API to measure the actual width of the text, resulting in better alignment and spacing for badges with varying character widths.

Speaking in code, this is how the old and new methods compare:
{ label: 'iiiiiiiiii', leftWidthOld: 86, leftWidth: 41 }
{ label: 'wwwwwwwwww', leftWidthOld: 86, leftWidth: 97 }
@ghostdevv ghostdevv added this pull request to the merge queue Feb 14, 2026
@ghostdevv ghostdevv removed this pull request from the merge queue due to a manual request Feb 14, 2026
@ghostdevv
Copy link
Contributor

actually will wait for current renovate PRs to go in, then merge

@ghostdevv ghostdevv added this pull request to the merge queue Feb 14, 2026
Merged via the queue into npmx-dev:main with commit b03fb88 Feb 14, 2026
18 checks passed
@wojtekmaj wojtekmaj deleted the text-measurement branch February 14, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants